﻿namespace Pyramid.Hardware
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class BusLink
    {
        private LineLink[] _outputBus;
        private ILine[] _inputBus;

        public BusLink(int busSize)
            : this(FixedInputLine.CreateBus(0, busSize).Cast<ILine>())
        {
        }

        public BusLink(IEnumerable<ILine> inputBus)
        {
            _outputBus = new LineLink[inputBus.Count()];

            _inputBus = inputBus.ToArray();

            for (int lineIndex = 0; lineIndex < _outputBus.Length; lineIndex++)
                _outputBus[lineIndex] = new LineLink(_inputBus[lineIndex]);
        }

        public IEnumerable<ILine> InputBus
        {
            get
            {
                return _inputBus;
            }
            set
            {
                if (value.Count() != _outputBus.Length)
                    throw new ArgumentException("InputBus must be of the same size as the existing OutputBus.", "value");

                _inputBus = value.ToArray();

                for (int lineIndex = 0; lineIndex < _outputBus.Length; lineIndex++)
                    _outputBus[lineIndex].InputLine = _inputBus[lineIndex];
            }
        }

        public IEnumerable<ILine> OutputBus
        {
            get
            {
                return _outputBus;
            }
        }
    }
}